//
//  OMShoppingcartHeaderView.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/1/4.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMShoppingcartHeaderView.h"
#import "MainConfigure.h"
@interface OMShoppingcartHeaderView ()
@property (nonatomic, strong) UIView *topBar;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *checkBoxBtn;
@property (nonatomic, strong) UIView *bottomLine;
@end

@implementation OMShoppingcartHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self configure];
    }
    return  self;
}

- (void)checkBoxBtnClick
{
    _checkBoxBtn.selected = !self.checkBoxBtn.selected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectCheckBoxAtSection:withIsCheck:)]) {
        [self.delegate didSelectCheckBoxAtSection:self.section withIsCheck:self.checkBoxBtn.selected];
    }
}

- (void)configure
{
    [self.contentView addSubview:self.topBar];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.checkBoxBtn];
    //[self.contentView addSubview:self.bottomLine];
    self.contentView.backgroundColor = HEXCOLOR(@"#FAFAFA");
    [self setupConstraints];

}

- (void)setupConstraints
{


    [self.topBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
        make.height.equalTo(@(SM_SIZE(0)));
    }];


    [self.checkBoxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(25)));
        make.width.height.equalTo(@(SM_SIZE(40)));
        make.centerY.equalTo(self.titleLabel.mas_centerY);
    }];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.checkBoxBtn.mas_right).offset(SM_SIZE(20));
//        make.right.equalTo(@(SM_SIZE(-20)));
        make.top.equalTo(self.topBar.mas_bottom);
        make.bottom.equalTo(@0);
    }];

    /*
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
        make.height.equalTo(@(0));
    }];
     */
}

#pragma mark - Getters
- (UIView *)bottomLine
{
    if (!_bottomLine) {
        _bottomLine = [UIView new];
//        _bottomLine.backgroundColor = SM_placeholdColor;

    }
    return _bottomLine;
}

- (UIView *)topBar
{
    if (!_topBar) {
        _topBar = [UIView new];
        _topBar.backgroundColor = SM_backGroundColor;
    }
    return _topBar;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = SM_blackColor;
        _titleLabel.font = SM_CFont(31);
    }
    return _titleLabel;
}

- (UIButton *)checkBoxBtn
{
    if (!_checkBoxBtn) {
        _checkBoxBtn = [UIButton new];
        [_checkBoxBtn setImage:[UIImage imageNamed:@"btn_shoppingcart"] forState:UIControlStateNormal];
        [_checkBoxBtn setImage:[UIImage imageNamed:@"btn_shoppingcart_hl"] forState:UIControlStateSelected];
        [_checkBoxBtn setTitleColor:SM_grayColor forState:UIControlStateNormal];
        [_checkBoxBtn.imageView setContentMode:UIViewContentModeScaleAspectFit];
//        [_checkBoxBtn addTarget:self action:@selector(checkBoxBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _checkBoxBtn.userInteractionEnabled = NO;
    }
    return _checkBoxBtn;
}

- (void)setTitle:(NSString *)title
{
    self.titleLabel.text = title;
}

+ (NSString *)identifier
{
    return NSStringFromClass(self.class);
}

- (void)setSm_selected:(BOOL)sm_selected
{
    self.checkBoxBtn.selected = sm_selected;
}

- (BOOL)sm_selected
{
    return self.checkBoxBtn.selected;
}

+ (CGFloat)headerHeight
{
    return SM_SIZE(90);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    if (point.x < CGRectGetMaxX(self.titleLabel.frame)) {
        [self checkBoxBtnClick];
    }
}
@end
